package services

import (
	"errors"
	"game-server/app/common/enum"
	"game-server/app/common/request"
	"game-server/app/common/response"
	"game-server/app/models"
	"game-server/global"
)

type categoryService struct {
}

var CategoryService = new(categoryService)

func (categoryService *categoryService) CreateCategory(params request.CategoryAddRequest) error {
	var count int64
	global.App.DB.Model(&models.Category{}).Where("name = ?", params.Name).Count(&count)
	if count > 0 {
		return errors.New("分类名称已存在")
	}

	category := models.Category{
		Name:   params.Name,
		Icon:   params.Icon,
		Image:  params.Image,
		Desc:   params.Desc,
		Sort:   params.Sort,
		Status: enum.Enabled,
	}

	if err := global.App.DB.Create(&category).Error; err != nil {
		return err
	}

	return nil
}

func (categoryService *categoryService) UpdateCategory(params request.CategoryUpdateRequest) error {
	// 检查分类是否存在
	var category models.Category
	if err := global.App.DB.First(&category, params.Id).Error; err != nil {
		return errors.New("分类不存在")
	}

	// 检查名称是否重复（排除自身）
	var count int64
	global.App.DB.Model(&models.Category{}).Where("name = ? AND id != ?", params.Name, params.Id).Count(&count)
	if count > 0 {
		return errors.New("分类名称已存在")
	}
	// 更新分类
	// category.Name = params.Name
	// category.Desc = params.Desc
	// category.Sort = params.Sort
	// category.Status = params.Status
	if params.Name != "" {
		category.Name = params.Name
	}
	if params.Icon != "" {
		category.Icon = params.Icon
	}
	if params.Image != "" {
		category.Image = params.Image
	}
	if params.Desc != "" {
		category.Desc = params.Desc
	}
	if params.Sort != nil {
		category.Sort = *params.Sort
	}
	if params.Status != nil {
		category.Status = *params.Status
	}

	if err := global.App.DB.Save(&category).Error; err != nil {
		return err
	}

	return nil
}

func (categoryService *categoryService) DeleteCategory(id uint) error {
	// 检查分类是否存在
	var category models.Category
	if err := global.App.DB.First(&category, id).Error; err != nil {
		return errors.New("分类不存在")
	}

	// 软删除分类
	if err := global.App.DB.Delete(&category).Error; err != nil {
		return err
	}

	return nil
}

func (categoryService *categoryService) GetCategoryList(params request.CategoryPageListRequest) (response.CategoryListResponse, error) {
	var res response.CategoryListResponse
	var categories []models.Category

	// 构建查询
	query := global.App.DB.Model(&models.Category{})

	// 条件查询
	if params.Name != "" {
		query = query.Where("name LIKE ?", "%"+params.Name+"%")
	}
	if params.Status != nil {
		query = query.Where("status = ?", *params.Status)
	}

	// 获取总数
	query.Count(&res.Total)

	// 分页查询
	offset := (params.Page - 1) * params.PageSize
	err := query.Offset(offset).
		Limit(params.PageSize).
		Where("deleted_at IS NULL").
		// Where("status =?", enum.Enabled).
		Order("sort DESC, id DESC").
		Find(&categories).Error

	if err != nil {
		return res, err
	}

	res.List = categories

	return res, nil
}

// GetAllCategory
func (categoryService *categoryService) GetAllCategory() ([]models.Category, error) {
	var categories []models.Category
	err := global.App.DB.Where("deleted_at IS NULL").
		// Where("status =?", enum.Enabled).
		Order("sort DESC, id DESC").
		Find(&categories).Error
	if err != nil {
		return nil, err
	}
	return categories, nil
}
